package com.etc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etc.entity.AsMatter;
import com.etc.entity.AsUser;
import com.etc.entity.UserImage;
import com.etc.service.IAsUserService;
import com.etc.util.LoginResponse;
import com.etc.util.MyJWTHutool;
import com.etc.util.SMTPUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 测试Controller
 * 
 * @author ruoyi
 * @date 2022-11-25
 */
@RestController
@RequestMapping("/user")
@CrossOrigin("*")
public class AsUserController
{
    @Autowired
    private IAsUserService asUserService;

    /**
     * 查询测试列表
     */

    @GetMapping("/list")
    public PageInfo<AsUser> list(@RequestParam(value = "keywords" , required = false,defaultValue = "")String keywords, @RequestParam(value = "offset",defaultValue = "1",required = false) int page, @RequestParam(value = "limit",defaultValue = "10",required = false) int limit)
    {

        Page<AsUser> list = asUserService.selectAsUserList(keywords,page,limit);
        PageInfo<AsUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 导出测试列表
     */


//    @PostMapping("/export")
//    public void export(HttpServletResponse response, AsUser asUser) throws IOException {
//        List<AsUser> list = asUserService.selectAsUserList(asUser);
//        ExcelUtil<AsUser> util = new ExcelUtil<AsUser>(AsUser.class);
//        util.exportExcel(response, list, "测试数据");
//    }

    @Autowired
    private Queue uploadUserImage;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @PostMapping("/uploadImage")
    public AjaxResult upload(MultipartFile myfile,String username){
        UserImage userImage = new UserImage(myfile, username);
        amqpTemplate.convertAndSend(uploadUserImage.getName(),userImage);
        return new AjaxResult();
    }

    /**
     * 获取测试详细信息
     */

    @GetMapping(value = "/{id}")
    public AsUser getInfo(@PathVariable("id") Long id)
    {
        return asUserService.selectAsUserById(id);
    }

    /**
     * 新增测试
     */


    @PostMapping("/add")
    public AjaxResult add(@RequestBody AsUser asUser)
    {
        return asUserService.insertAsUser(asUser)>0?AjaxResult.success("注册成功"):AjaxResult.success("账号已存在");
    }

    /**
     * 修改测试
     */


    @PutMapping
    public AjaxResult edit(@RequestBody AsUser asUser)
    {
        return asUserService.updateAsUser(asUser)>0?AjaxResult.success("修改成功"):AjaxResult.success("修改失败");
    }

    /**
     * 删除测试
     */


	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return AjaxResult.success();
    }

    @GetMapping("/login")
    public LoginResponse login(String userphone, String password){
        System.out.println(userphone+"  "+password);
        AsUser asUser = new AsUser();
        asUser.setUserphone(userphone);
        asUser.setPassword(password);
        AjaxResult ajaxResult = asUserService.selectAsUserLogin(asUser);
        AsUser data = (AsUser) ajaxResult.get("data");
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setData(data);
        loginResponse.setToken(MyJWTHutool.createToken(data.getId(),data.getUsername()));
        loginResponse.setMsg((String) ajaxResult.get("msg"));
        return loginResponse;
    }

    @PutMapping("/forgetpassword")
    public AjaxResult forgetpassword(@RequestBody AsUser asUser){
        String password=String.valueOf(UUID.randomUUID());
        asUser.setPassword(password);
        if (asUserService.updateAsUser(asUser)>0){
            SMTPUtil.sendEmail(asUser.getMail(),password);
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.success("修改失败");
        }

    }
}
